#include "RS485.h"

cGpio_T RS485EnGpio[6] = {
    {0},        //0-485
    {0},        //1-485
    {        
        .port = GPIOB,
        .num = 12,
        .pin = GPIO_Pin_12
    },              //2-485
    {        
        .port = GPIOB,
        .num = 1,
        .pin = GPIO_Pin_1
    },             //3-485
    {0},           //4-485, 因为RS485-4用了SWD的TCK，所以不使用
    {
        .port = GPIOB,
        .num = 3,
        .pin = GPIO_Pin_3
    },              //5-485
};

void RS485EN_init()
{
    int i;
    for(i=0;i<6;i++)
    {
        if(RS485EnGpio[i].port!=0)
        {
            GPIO_PortClock(RS485EnGpio[i].port, true);
            GPIO_PinConfigure(RS485EnGpio[i].port, RS485EnGpio[i].num, GPIO_OUT_PUSH_PULL, GPIO_MODE_OUT50MHZ);
        }
    }
}

void RS485_SetMode(USART_TypeDef *USARTx, u8 mode)
{
    if (USARTx == USART2)
    {
        if (mode == RS485Wr)
            GPIO_SetBits(RS485EnGpio[2].port, RS485EnGpio[2].pin);
        else
            GPIO_ResetBits(RS485EnGpio[2].port, RS485EnGpio[2].pin);
    }
    else if (USARTx == USART3)
    {
        if (mode == RS485Wr)
            GPIO_SetBits(RS485EnGpio[3].port, RS485EnGpio[3].pin);
        else
            GPIO_ResetBits(RS485EnGpio[3].port, RS485EnGpio[3].pin);
    }
    else if (USARTx == UART5)
    {
        if (mode == RS485Wr)
            GPIO_SetBits(RS485EnGpio[5].port, RS485EnGpio[5].pin);
        else
            GPIO_ResetBits(RS485EnGpio[5].port, RS485EnGpio[5].pin);
    }
}

